package com.smt.modules.app.utils;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.smt.modules.bus.entity.BillEntity;
import com.smt.modules.bus.service.BillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 当天奖励领取
 *
 * @author zhangyi
 * @date 2018/10/30 18:23
 */
@Component
public class CurrentDayRewardUtil {

    @Autowired
    private BillService billService;

    /**
     *
     * @param sR
     * @param userId
     */
    public void issueRewards(String sR, Long userId) {
        //改变账单领取状态
        List<BillEntity> billes = billService.selectList(new EntityWrapper<BillEntity>()
                .eq("bus_business_id", userId)
                .eq("type", 0)
                .eq("expenditure", 1)
                .eq("is_reward", 0));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss");
        String format = sdf.format(new Date(System.currentTimeMillis()));
        String toDay = format.split(" ")[0];
        List<BillEntity> billList = new ArrayList<>();
        //账单筛选
        if (billes.size() != 0) {
            billes.stream().forEach(b -> {
                SimpleDateFormat timeFortmer = new SimpleDateFormat("yyyy-MM-dd KK:mm:ss");
                String formatt = timeFortmer.format(b.getTransactionTime());
                String date = formatt.split(" ")[0];
                if (date.equals(toDay)) {
                    billList.add(b);
                }
            });
            //当天日常任务
            billList.stream().forEach(b -> {
                if (b.getIsReward() == 0 && sR.equals(b.getBillTitle())) {
                        BillEntity bill = new BillEntity();
                        bill.setId(b.getId());
                        bill.setIsReward(1);
                        billService.updateById(bill);
                }
            });
        }
    }
}